﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MyTestProject
{
    public static class ArffWriter
    {
        public static void WriteTFIDF(string fileToCreate, List<ICheckable> dataset,
            Dictionary<string, double> idf, List<string> features)
        {
            try
            {
                StringBuilder sbArffFile = new StringBuilder();
                string items = "";
                sbArffFile.Append("@RELATION documents");
                sbArffFile.AppendLine();
                sbArffFile.AppendLine();

                sbArffFile.Append("@ATTRIBUTE ");
                sbArffFile.Append("DocName {");

                foreach (var item in dataset)
                {
                    items += item.ID + ",";
                }

                items = items.TrimEnd(',');
                sbArffFile.Append(items);
                sbArffFile.Append("}");

                sbArffFile.AppendLine();


                foreach (string feature in features)
                {
                    sbArffFile.Append("@ATTRIBUTE ");
                    sbArffFile.Append(feature);
                    sbArffFile.Append(" NUMERIC ");
                    sbArffFile.AppendLine();
                }

                sbArffFile.AppendLine();
                sbArffFile.Append("@DATA");
                sbArffFile.AppendLine();
                sbArffFile.AppendLine();

                foreach (var item in dataset)
                {
                    sbArffFile.Append(item.ID);
                    foreach (string feature in features)
                    {
                        if (item.TF.ContainsKey(feature))
                        {
                            sbArffFile.Append("," + item.TF[feature] * idf[feature]);
                        }
                        else
                        {
                            sbArffFile.Append(",0");
                        }
                    }
                    sbArffFile.AppendLine();
                }

                StreamWriter writer = File.CreateText(fileToCreate);
                writer.Write(sbArffFile);
                writer.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.Message);
            }
        }
    }
}
